package edu.hawaii.myisern.model;

import java.util.List;
import java.util.ArrayList;
import net.sourceforge.stripes.util.Log;
import edu.hawaii.myisern.researchers.jaxb.Researcher;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;

/**
 * Reports on data incompleteness between Organizations, Researchers, & Collaborations.
 *
 * @author Lisa Chen
 */
public class Report {

  /** Logging debug output. */
  protected static Log log = Log.getInstance(Report.class);

  /** Default constructor. */
  public Report() {
    // do nothing
  }

  /**
   * Get Researcher without valid Organization Ids.
   *
   * @return List of Researchers without an Org or valid Org Ids.
   */
  public List<Researcher> getResesarchersWithoutOrgs() {
    List<Researcher> resWithoutOrgsErrors = new ArrayList<Researcher>();
    IsernManager im = new IsernManager();
    for (Researcher res : im.getResearchers()) {
      if (res.getOrganization() == null) {
        resWithoutOrgsErrors.add(res);
        log.debug("Reseacher without Org added1 = " + res.getName()) ;
        continue;
      }
      Id orgId = new Id(res.getOrganization());
      boolean found = false;
      for (Organization org : im.getOrganizations()) {
        if (orgId.equals(new Id(org.getName()))) {
          found = true;
          break;
        }
      }
      if (!found) {
        resWithoutOrgsErrors.add(res);
        log.debug("Reseacher without Org added2 = " + res.getName()) ;
      }
    }
    return resWithoutOrgsErrors;
  }

  /**
   * Get Organizations without Researchers by IDs.
   *
   * @return List of Organizations without affiliated researchers.
   */
  public synchronized List<Organization> getOrgsWithoutResesarchers() {
    List<Organization> orgsWithoutResesarchErrors = new ArrayList<Organization>();
    IsernManager im = new IsernManager();
    for (Organization org : im.getOrganizations()) {
      if (org.getContact() == null) {
        orgsWithoutResesarchErrors.add(org);
        log.debug("Org without Researcher added1 = " + org.getName()) ;
        continue;
      }
      Id researcherId = new Id(org.getContact());
      boolean found = false;
      for (Researcher res : im.getResearchers()) {
        if (researcherId.equals(new Id(res.getName()))) {
          found = true;
          break;
        }
      }
      if (!found) {
        orgsWithoutResesarchErrors.add(org);
        log.debug("Org without Researcher added2 = " + org.getName()) ;
      }
    }
    return orgsWithoutResesarchErrors;
  }

  /**
   * Get Collaborations without valid Organization Ids.
   *
   * @return List of Collaborations without an Org or valid Org Ids.
   */
  public synchronized List<Collaboration> getCollabsWithoutOrgs() {
    List<Collaboration> collabsWithoutOrgsErrors = new ArrayList<Collaboration>();
    IsernManager im = new IsernManager();

    for (Collaboration collab : im.getCollaborations()) {
      // if collaboration is missing a name
      if (collab.getName() == null) {
        collabsWithoutOrgsErrors.add(collab);
        log.debug("Collab without Org added1 = " + collab.getName()) ;
        continue;
      }
      // Validate the list of organizations for the current collaboration
      for (String collabOrg :
        collab.getCollaboratingOrganizations().getCollaboratingOrganization()) {
        Id orgId = new Id(collabOrg);
        boolean found = false;
        for (Organization org : im.getOrganizations()) {
          if (orgId.equals(new Id(org.getName()))) {
            found = true;
            break;
          }
        }
        // if no valid organization(s) or missing organization(s)
        if (!found) {
          collabsWithoutOrgsErrors.add(collab);
          log.debug("Collab without Org added2 = " + collab.getName()) ;
        }
      }
    }
    return collabsWithoutOrgsErrors;
  }

  /**
   * Get Collaborations without outcomes.
   *
   * @return List of Collaborations without outcomes.
   */
  public synchronized List<Collaboration> getCollabsWithoutOutcomes() {
    List<Collaboration> collabsWithoutOutcomes = new ArrayList<Collaboration>();
    IsernManager im = new IsernManager();

    for (Collaboration collab : im.getCollaborations()) {
      // if collaboration is missing a name
      if (collab.getName() == null) {
        collabsWithoutOutcomes.add(collab);
        log.debug("Collab without Outcome added1 = " + collab.getName()) ;
        continue;
      }
      // if outcome is missing, add it to the list
      if (collab.getOutcomeTypes().getOutcomeType() == null 
          || collab.getOutcomeTypes().getOutcomeType().size() < 1) {
        collabsWithoutOutcomes.add(collab);
        log.debug("Collab without Outcome added2 = " + collab.getName()) ;
        continue;
      }
    }
    return collabsWithoutOutcomes;
  }
}
